mod physics;
mod visual;
mod mover;
mod nervousfly;
mod swimmingfish;
mod hoppingbunny;
mod slitheringsnake;

use ggez::{Context, GameResult, GameError, graphics, glam::Vec2};
use rand::Rng;

pub struct State{
    movers: Vec<Box<dyn mover::Mover>>
}

impl State{
    pub fn new(ctx: &mut Context) -> State {
        let mut movers: Vec<Box<dyn mover::Mover>> = vec![];
        let mut rng = rand::thread_rng();
        let width = ctx.gfx.window().inner_size().width as f32;
        let heigth = ctx.gfx.window().inner_size().height as f32;
        for _i in 0..200 {
            let location = Vec2{x: rng.gen_range(0.0..width ),y: rng.gen_range(0.0..heigth)};
            let p = rng.gen_range(0.0..1.0);
            if p < 0.2 {
                let mover = swimmingfish::SwimmingFish::new(location);
                movers.push(Box::new(mover));
            } else if p < 0.4{
                let mover = hoppingbunny::HoppingBunny::new(location);
                movers.push(Box::new(mover));
            } else if p < 0.5{
                let mover = slitheringsnake::SlitheringShake::new(location);
                movers.push(Box::new(mover));
            } else {
                let mover = nervousfly::NervousFly::new(location);
                movers.push(Box::new(mover));
            }
        }
        State{ movers }
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, ctx: &mut Context) -> GameResult {
        for mover in &mut self.movers {
            mover.update(ctx);
        }
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);
        for mover in &mut self.movers {
            mover.display(ctx, &mut canvas);
        }
        canvas.finish(ctx)?;
        Ok(())
    }
}